namespace bt {
    /**
     * 复合节点基类
     *
     * @export
     * @class Composite
     * @extends {BaseNode}
     */
    export class Composite extends BaseNode {
        constructor(params: any) {
            super(params);
            params = params || {};
            this.category = CategoryType.COMPOSITE;
            this.name = 'Composite';
            this.children = params.children || [];
        }
    
    }

    /**
     * 选择节点
     *
     * @export
     * @class Priority
     * @extends {Composite}
     */
    export class Priority extends Composite {
        constructor(params?: any) {
            super(params);
            this.name = 'Priority';
        }
        tick(tick: Tick) : ActionStatus {
            for (let i = 0; i < this.children.length; i++) {
                let status = this.children[i]._execute(tick);
                if (status !== ActionStatus.FAILURE) {
                    return status;
                }
            }
            return ActionStatus.FAILURE;
        }
    }

    /**
     * 顺序节点
     *
     * @export
     * @class Sequence
     * @extends {Composite}
     */
    export class Sequence extends Composite {
        constructor(params?: any) {
            super(params);
            this.name = 'Sequence';
        }
        tick(tick: Tick) : ActionStatus {
            for (let i = 0; i < this.children.length; i++) {
                let status = this.children[i]._execute(tick);
                if (status !== ActionStatus.SUCCESS) {
                    return status;
                }
            }
            return ActionStatus.SUCCESS;
        }
    }

    /**
     * 带记录的选择节点
     *
     * @export
     * @class MemPriority
     * @extends {Composite}
     */
    export class MemPriority extends Composite {
        constructor(params?: any) {
            super(params);
            this.name = 'MemPriority';
        }
    
        open(tick: Tick) {
            tick.blackboard.set('runningChild', 0, tick.tree.id, this.id);
        }
    
        tick(tick: Tick) : ActionStatus {
            let child = tick.blackboard.get('runningChild', tick.tree.id, this.id);
            for (let i = child; i < this.children.length; i++) {
                let status = this.children[i]._execute(tick);
                if (status !== ActionStatus.FAILURE) {
                    if (status === ActionStatus.RUNNING) {
                        tick.blackboard.set('runningChild', i, tick.tree.id, this.id);
                    }
                    return status;
                }
            }
            return ActionStatus.FAILURE;
        }
    
    }

    /**
     * 带记录的顺序节点
     *
     * @export
     * @class MemSequence
     * @extends {Composite}
     */
    export class MemSequence extends Composite {
        constructor(params?: any) {
            super(params);
            this.name = 'MemSequence';
        }
    
        open(tick: Tick) {
            tick.blackboard.set('runningChild', 0, tick.tree.id, this.id);
        }
    
        tick(tick: Tick) : ActionStatus {
            let child = tick.blackboard.get('runningChild', tick.tree.id, this.id);
            for (let i = child; i < this.children.length; i++) {
                let status = this.children[i]._execute(tick);
                if (status !== ActionStatus.SUCCESS) {
                    if (status === ActionStatus.RUNNING) {
                        tick.blackboard.set('runningChild', i, tick.tree.id, this.id);
                    }
                    return status;
                }
            }
            return ActionStatus.SUCCESS;
        }
    
    }
}